define([
  'jquery',
  'jqueryui',
  'underscore',
  'backbone',
  'lightboxme',  
  'msgbox',
  'collections/response/ExistingTemplatesCollection',
  'views/response/ManageResponseListView',
  'text!templates/response/manageResponseTemplate.html'
], function($,JQueryUI, _, Backbone,LightBoxMe,MsgBox,ExistingTemplatesCollection, ManageResponseListView,manageResponseTemplate){

  var ManageResponseView = Backbone.View.extend({
    el: $(".mainpanel"),
    initialize: function(){/*
    	var ReferModel = Backbone.Model.extend({url:'/referPatient'});
    	this.model = new ReferModel();
    */},
    events: {
    	/*'click #searchRefer' : 'referButton'*/
    	'click .uploadnew' : 'uploadButton'
    	} ,
    render: function(){
  	  
	      var compiledTemplate = _.template( manageResponseTemplate, null);
	      this.$el.html(compiledTemplate);
	  	  
	  	 $('.file-wrapper input[type=file]')
	  	  .bind('change', function(){
	  		  var $this = $(this),
		        $val = $this.val(),
		        valArray = $val.split('\\');
		        newVal = valArray[valArray.length-1];
		  	  $('#fileName').val(newVal);
	  	  });

    	  var existingTemplatesCollection = new ExistingTemplatesCollection();
    	   var lOptions = {};
           var self=this;
           lOptions.success = function(){
           	var manageResponseListView = new ManageResponseListView({ collection: existingTemplatesCollection});
           	manageResponseListView.render();
           	//self.manageResponseListView =manageResponseListView; 
           };
           existingTemplatesCollection.fetch(lOptions);


    },
    uploadButton: function(){
    	if($("#problemDesc").val() == ""||$('#fileName').val()==""){
   		 $.msgBox({
			    title:"Appment",
			    content:"Problem Description and file are mandatory!",
			    type:"error"
			});
   	}else{
       	var otherdata ={};
       	otherdata["topic"]=$("#problemDesc").val();
       	otherdata["id"]=0;
	  	        $.ajax("postUserTemplate", {
	  	            data: otherdata,
	  	            dataType: "json",
	  	          type: "POST",
	  	            files: $("#newResponseForm").find(":file"),
	  	            iframe: true,
	  	            processData: false
	  	          }).always(function() {
	  	        	  
	  	        	 $.msgBox({
	         			    title:"Appment",
	         			    content:"Saved Successfully!",
	         			    type:"info",
	  				    success: function (result) {
	  				   	  var existingTemplatesCollection = new ExistingTemplatesCollection();
	  			    	   var lOptions = {};
	  			           lOptions.success = function(){
	  			           	var manageResponseListView = new ManageResponseListView({ collection: existingTemplatesCollection});
	  			           	manageResponseListView.render();
	  			           	//self.manageResponseListView =manageResponseListView; 
	  			           };
	  			           existingTemplatesCollection.fetch(lOptions);

	  				    }
	         			});
	  	          }).done(function(data) {
	            		

	  	          });
   		
   	}
    }
  });

  return ManageResponseView;
});
